package cn.yfj.basic.controller;

import cn.yfj.basic.config.BusinessException;
import cn.yfj.basic.service.IVerifyCodeService;
import cn.yfj.basic.util.AjaxResult;
import cn.yfj.basic.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/verifyCode")
public class ImageVerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}") //所有要获取图片验证码都可以使用
    public AjaxResult imgCode(@PathVariable("key")String key){
        try {
            //生成base64编码之后的图片string
            String base64Str = verifyCodeService.imageCode(key);
            //将base64编码之后的图片string
            return AjaxResult.me().setResultobj(base64Str);
        } catch (BusinessException e) {//捕获自定义的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }

}